<?php

class game_video_auto_cache extends auto_cache {

    private $key = "game:video:";

    public function load($param) {
        $city = strim($param['area_type']);

        $order = strim($param['order']); //排序字段
        $sort = strim($param['sort']); //排序方式
        $is_robot = intval($param['is_robot']); // 是否机器人

        if ($city == '热门' || $city == 'null') {
            $city = '';
        }
        $this->key .= $city . '_' . $is_robot;
        if ($order) {
            $this->key .= '_' . $order;
        }
        if ($sort) {
            $this->key .= '_' . $sort;
        }
        $key_bf = $this->key . '_bf';
        $list = $GLOBALS['cache']->get($this->key, true);
        $list = false;
        if ($list === false) {
            $is_ok = $GLOBALS['cache']->set_lock($this->key);
            if (!$is_ok) {
                $list = $GLOBALS['cache']->get($key_bf, true);
            } else {
                $sql = "SELECT v.id AS room_id, v.sort_num, v.group_id, v.user_id, v.city, v.title, v.cate_id, v.live_in, v.video_type, v.create_type, v.room_type,
						(v.robot_num + v.virtual_watch_number + v.watch_number) as watch_number, u.head_image,u.thumb_head_image,v.live_image, v.xpoint,v.ypoint,
						u.v_type, u.v_icon, u.nick_name,u.user_level,v.is_live_pay,v.live_pay_type,v.live_fee,u.create_time as user_create_time FROM " . DB_PREFIX . "video v 		
                    			LEFT JOIN " . DB_PREFIX . "user u ON u.id = v.user_id where v.live_in in (1,3) and u.is_hot_on =0";
                $robotUids = get_game_main_robot_uid();
                if ($is_robot) {
                    if ($robotUids) {
                        $list = $GLOBALS['db']->getAll($sql . ' and v.user_id in (' . implode(',', $robotUids) . ')');
                    }
                } else {
                    if ($robotUids) {
                        $sql .= ' and v.user_id not in (' . implode(',', $robotUids) . ')';
                    }
                    if ($city != '') {
                        $sql .= " and v.province = '" . $city . "'";
                    }
                    if ($order) {
                        $sql .= " order by v." . $order;
                        if ($sort) {
                            $sql .= " " . $sort;
                        }
                    } else {
                        $sql .= "  order by v.sort_num desc,v.sort desc";
                    }
                    $list = $GLOBALS['db']->getAll($sql, true, true);
                }
                foreach ($list as $k => $v) {
                    //判断用户是否为今日创建的新用户，是：1，否：0
                    if (date('Y-m-d') == date('Y-m-d', $list[$k]['user_create_time'] + 3600 * 8)) {
                        $list[$k]['today_create'] = 1;
                    } else {
                        $list[$k]['today_create'] = 0;
                    }

                    if ($v['live_image'] == '') {
                        $list[$k]['live_image'] = get_spec_image($v['head_image']);
                        $list[$k]['head_image'] = get_spec_image($v['head_image']);
                    } else {
                        $list[$k]['live_image'] = get_spec_image($v['live_image']);
                        $list[$k]['head_image'] = get_spec_image($v['head_image'], 150, 150);
                    }
                    if ($v['thumb_head_image'] == '') {
                        $list[$k]['thumb_head_image'] = get_spec_image($v['head_image'], 150, 150);
                    } else {
                        //$list[$k]['thumb_head_image'] = get_abs_img_root($v['thumb_head_image']);
                        $list[$k]['thumb_head_image'] = get_spec_image($v['thumb_head_image'], 150, 150);
                    }
                    $list[$k]['live_state'] = '';
                    if ($v['live_in'] == 1) {
                        $list[$k]['live_state'] = '直播';
                    } else {
                        $list[$k]['live_state'] = intval($v['is_gather']) == 1 ? '直播' : '回播';
                    }
                }

                $GLOBALS['cache']->set($this->key, $list, 10, true);

                $GLOBALS['cache']->set($key_bf, $list, 86400, true); //备份
                //echo $this->key;
            }
        }

        if ($list == false)
            $list = array();

        return $list;
    }

    public function rm() {

        //$GLOBALS['cache']->clear_by_name($this->key);
    }

    public function clear_all() {

        $GLOBALS['cache']->clear_by_name($this->key);
    }

}

?>